package mysql

import (
	"manuscript-service/servicerpc/servicerpc"

	"gorm.io/gorm"
)

type Article struct {
	gorm.Model
	Title   string `gorm:"type:varchar(50);not null;comment:'稿纸标题'"`
	Likes   int64  `gorm:"type:int(11);not null;comment:'稿纸点赞量'"`
	Image   string `gorm:"type:varchar(255);not null;comment:'稿纸图片'"`
	ClassId int64  `gorm:"type:int(11);not null;comment:'分类id'"`
}

func (a *Article) ArticleAdd(db *gorm.DB) error {
	return db.Debug().Create(a).Error
}

func (a *Article) FindArticleByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(a).Error
}

func ArticleList(db *gorm.DB, in *servicerpc.ArticleListRequest) ([]*servicerpc.ArticleList, error) {
	var list []*servicerpc.ArticleList
	err2 := db.Debug().Model(&Article{}).
		Select("articles.id as article_id", "title", "likes", "image", "name as class_name").
		Joins("LEFT JOIN  classes ON articles.class_id=classes.id").
		Find(&list).Error
	return list, err2
}
